<?php
namespace src\logic;
use \classlib\core as C;
class lshare {
	//定义头像地址信息
	const AVTAR 	= '/pshow/avtar.png';
	const MAXSIZE   = 2097152; //2MB
	const STARRATIO = 100; //1加币=100星币
	const CANRATIO  = 100; //1加币=100加分
	const FCANRATIO = 1;   //1加分=1星币
	
	//加载js语言包设置
	public static function lang($js='lang.js') {
		$lang = C\lang::lng('en');
		return '/static/v1.0/lang/'.$lang.'/'.$js;	
	}
	
	//账号是邮箱的话使用邮箱登录
	public static function email_shilfter($email) {
		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
			return '';
		}
		list($uname, $subfix) = explode('@', $email);
		$nlen  = strlen($uname);
		$uname = ($nlen > 4)? substr($uname, 0, $nlen - 4).str_repeat('*', 4):str_repeat('*', $nlen);
		return $uname.'@'.$subfix;
	}
	
	/**
	 * 随机获取一个静态资源地址
	 * @return $string
	 */
	public static function uri() {
		static $idx  = 0;
		static $uris = null;
		if (is_null($uris)) {
			$uris = C\config::load('static', false);
		}
		return $uris[$idx++ % count($uris)];	
	}
	
	/**
	 * 获取语言包资料信息
	 * @return array $lng
	 */
	public static function i18n($ini='web') {
		$lng  = array();
		$lang = C\lang::inst($ini);
		$segs = func_get_args();
		$segs = array_slice($segs, 1);
		foreach ($segs as $seg) {
			$lng += $lang->get($seg);
		}
		return $lng;
	}
	
	/**
	 * 获取订阅的广告资料信息
	 * @return array $list 
	 */
	public static function ads($use=0, $lng='en') {
		$where = array('lng'=>$lng, '`use`'=>$use,
			array('expire', C\config::$nowtime, \classlib\orm::OP_GT));
		$field = 'id,title,src,link,apns';
		$list  = C\model::load('main.pl.ads')
			->get_list(0, -1, $where, $field, 'rank', \classlib\orm::DESC);
		return $list;
	}
	
	/**
	 * 解析参数id资料信息列表 返回数组
	 * @param string $key
	 * @return array $ids
	 */
	public static function ids($key='ids') {
		$ids = \classlib\input::get_post($key);
		$ids = explode(',', $ids);
		$ids = array_unique($ids);
		$ids = array_filter($ids, 'is_numeric');
		return $ids;
	}
	
	/**
	 * 设置时区返回日期格式
	 * @param string $email
	 * @return string
	 */
	public static function timezone($timestamp, $zone='America/Vancouver', $suffix='PST') {
		$date = new \DateTime();
		$date->setTimezone(new \DateTimeZone($zone));
		$date->setTimestamp($timestamp);
		return $date->format('Y-m-d H:i:s').' '.$suffix;
	}
	
	/**
	 * 获取礼物信息
	 */
	public static function gift($lng='en') {
		$fields = 'eid as id,reglv as lv,name,isguard,icon,swf,gif,`desc`,coinprice as coin';
		$where  = array('status'=>1, 'hidden'=>0, 'lng'=>$lng);
		$list   = C\model::load('main.inc.gift')->get_list(0, -1, $where, $fields, '`rank`', \classlib\orm::ASC);
		return $list;
	}
	
	/**
	 * 增加守护的图标地址
	 * @param string $lng
	 */
	public static function vipsquad($lng='en') {
		$vips   = array();
		$where  = array('lng'=>$lng);
		$fields = 'eid as id,name,iconsrc as icon';
		$list   = C\model::load('main.inc.guard')->get_list(0, -1, $where, $fields);
		foreach ($list as $item) {
			$vips[$item['id']] = $item;
		}
		return $vips;
	}
	
	/**
	 * 新增座驾资料信息
	 * @param string $lng
	 */
	public static function motor($lng='en') {
		$motor  = array();
		$fields = 'id, icon, swf, name, isshow, app, coinprice, beanprice';
		$where  = array('status'=>1, 'lng'=>$lng);
		$list   = C\model::load('main.inc.motor')->get_list(0, -1, $where, $fields, 'rank', \classlib\orm::DESC);
		foreach ($list as $item) {
			$motor[$item['id']] = $item;
		}
		return $motor;
	}
	
	/**
	 * 自定义表情配置
	 * @return array
	 */
	public static function face() {
		$face = array();
		return $face;
	}
}